﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_ToolTypeEdit_Form : DevExpress.XtraEditors.XtraForm
    {
        private TS_ToolType m_oToolType = new TS_ToolType();

        private string m_sParentName = "";

        public Tool_System_ToolTypeEdit_Form()
        {
            InitializeComponent();
            //pe_tooltype.Size = new Size(400, 400);
        }

        public string SParentName { get => m_sParentName; set => m_sParentName = value; }
        public TS_ToolType OToolType { get => m_oToolType; set => m_oToolType = value; }

        private void Tool_System_ToolTypeEdit_Form_Load(object sender, EventArgs e)
        {
            te_name.Text = m_oToolType.SName;
            te_cycle.Text = m_oToolType.IRepairCycle.ToString();
            pe_tooltype.Image = Utility.ChangeStringToImage(m_oToolType.SPicture);
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            m_oToolType.SName = te_name.Text;
            m_oToolType.IRepairCycle = int.Parse(te_cycle.Text);
            m_oToolType.SPicture = Utility.ChangeImageToString(pe_tooltype.Image);

            if (m_oToolType.IID == 0)
            {
                if (ToolDataMgr.GetInstance().AddToolType(m_oToolType))
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
                if (ToolDataMgr.GetInstance().ModifyToolType(m_oToolType))
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void pe_tooltype_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofdPic = new OpenFileDialog();
            ofdPic.Filter = "BMP(*.BMP);JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.bmp;*.jpg;*.jpeg;*.gif";
            ofdPic.FilterIndex = 1;
            ofdPic.FileName = "";
            if (ofdPic.ShowDialog() == DialogResult.OK)
            {
                string sPicPaht = ofdPic.FileName.ToString();
                Bitmap bmPic = new Bitmap(sPicPaht);
                Point ptLoction = new Point(bmPic.Size);
                if (ptLoction.X > pe_tooltype.Size.Width || ptLoction.Y > pe_tooltype.Size.Height)
                {
                    //图像框的停靠方式  
                    //pe_tooltype.Dock = DockStyle.Fill;
                    //图像充滿图像框，並且图像維持比例  
                    pe_tooltype.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze;
                }
                else
                {
                    //图像在图像框置中  
                    pe_tooltype.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }

                //LoadAsync：非同步转入图像  
                pe_tooltype.LoadAsync(sPicPaht);
            }
        }

        private void pe_tooltype_LoadCompleted(object sender, EventArgs e)
        {
            Image image = Utility.ChangePhotoSize(pe_tooltype.Image, 100, 100);
            pe_tooltype.Image = image;
        }

        //private void btn_picture_Click(object sender, EventArgs e)
        //{
        //    OpenFileDialog ofdPic = new OpenFileDialog();
        //    ofdPic.Filter = "BMP(*.BMP);JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.bmp;*.jpg;*.jpeg;*.gif";
        //    ofdPic.FilterIndex = 1;
        //    ofdPic.FileName = "";
        //    if (ofdPic.ShowDialog() == DialogResult.OK)
        //    {
        //        string sPicPaht = ofdPic.FileName.ToString();
        //        Bitmap bmPic = new Bitmap(sPicPaht);
        //        Point ptLoction = new Point(bmPic.Size);
        //        if (ptLoction.X > pe_tooltype.Size.Width || ptLoction.Y > pe_tooltype.Size.Height)
        //        {
        //            //图像框的停靠方式  
        //            //pcbPic.Dock = DockStyle.Fill;  
        //            //图像充滿图像框，並且图像維持比例  
        //            //pe_tooltype.SizeMode = PictureBoxSizeMode.Zoom;
        //        }
        //        else
        //        {
        //            //图像在图像框置中  
        //            //pe_tooltype.SizeMode = PictureBoxSizeMode.CenterImage;
        //        }

        //        //LoadAsync：非同步转入图像  
        //        pe_tooltype.LoadAsync(sPicPaht);
        //    }
        //}
    }
}